package forecast.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Set;

import javax.security.auth.Subject;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import forecast.domain.Activity;
import forecast.domain.Forecast;
import forecast.security.SecurityUtils;
import forecast.service.ActivityService;
import forecast.service.ForecastService;

/**
 * Controller to create, list and update Activities
 * 
 * @author pjohanss
 * 
 */
@Controller
@SessionAttributes("forecast")
public class ForecastController {
	
	//Ylimäärästä
	
	
	
	
	//Loppuu tähän
	

	@Autowired
	ForecastService forecastService;
	
	@Autowired
	ActivityService activityService;

	/**
	 * Return index page
	 * 
	 * @return
	 */
	@RequestMapping(value = "/forecast", method = RequestMethod.GET)
	public String index(Model model) {
		/**if (forecastService.listForecasts() != null) {
			Set forecasts = forecastService.listForecasts();
			model.addAttribute("forecasts", forecasts);
		}
		Set activities = activityService.listActivities();
		model.addAttribute("activities", activities);
		return "forecast/index";
		Set forecasts = forecastService.listForecasts();
		model.addAttribute("forecasts", forecasts);*/
		Set <Activity> activities = activityService.listActivities();
		double totalEka = 0;
		double totalToka = 0;
		double totalKolmas = 0;
		double totalNeljas = 0;
		
		for(Activity activity:activities) {			
			totalEka += activity.getEka();
			totalToka += activity.getToka();
			totalKolmas += activity.getKolmas();
			totalNeljas += activity.getNeljas();
		}
		
		
		model.addAttribute("totalEka", totalEka);
		model.addAttribute("totalToka", totalToka);
		model.addAttribute("totalKolmas", totalKolmas);
		model.addAttribute("totalNeljas", totalNeljas);
		
		model.addAttribute("activities", activities);
		return "forecast/index";
		
	}

	/**
	 * Return edit page with empty Activity object (to create a new)
	 * 
	 * @return
	 */
	@RequestMapping(value = "/forecast/create", method = RequestMethod.GET)
	public String create(@RequestParam("activityId") String activityId, Model m) {
		m.addAttribute("activityId", activityId);
		return "forecast/create";
	}

	@ModelAttribute("forecast")
	public Forecast createActivity() {
		return new Forecast();
	}

	/**
	 * Handles form submit of an Forecast object. Validates and creates a new
	 * object
	 * 
	 * @param activity
	 * @param result
	 * @return
	 */
	@RequestMapping(value = "/forecast", method = RequestMethod.POST)
	public String submit(@Valid Forecast forecast, @Valid @RequestParam("eka") String eka, @Valid
			@RequestParam("toka") String toka, @Valid @RequestParam("kolmas") String kolmas,  @RequestParam("activityId") long activityId,
			@Valid @RequestParam("neljas") String neljas, BindingResult result, Model m,SessionStatus sessionStatus) {
		if (result.hasErrors()) {
			return "/forecast/index";
		} else {
			Activity activity = activityService.getActivity(activityId);
			
			activity.setEka(Double.parseDouble(eka));
			activity.setToka(Double.parseDouble(toka));
			activity.setKolmas(Double.parseDouble(kolmas));
			activity.setNeljas(Double.parseDouble(neljas));

			
			Subject s = SecurityUtils.getCurrentSubject();
			forecast.setUserName(SecurityUtils.getUserName(s));
			activityService.updateActivity(activity);
			//Remove forecast object from session
			sessionStatus.setComplete();
			// Return to list view
			return index(m);
		}
	}

	@InitBinder
	public void initBinder(WebDataBinder webDataBinder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		dateFormat.setLenient(false);
		webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

}
